/**
 * Copyright (c) 2014-now, secondriver (secondriver@yeah.net).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package secondriver.jfinal.rack.kit;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;

/**
 * PropertiesKit for handling properites file key-value.
 * 
 * @author secondriver
 * @time 2014年6月30日
 */
public class RockProperitesKit {
	/**
	 * 从指定文件名中获取Properties对象
	 * 
	 * @param filename
	 *            物理文件名,如：/home/java/sys.properties D:\home\java\sys.properties
	 * @return Properties对象
	 */
	public static Properties getProperty(String filename) {
		Properties p = new Properties();
		try {
			p.load(new FileReader(new File(filename)));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return p;
	}

	/**
	 * 从指定文件对象中获取Properties对象
	 * 
	 * @param file
	 *            指定文件对象
	 * @return Properties对象
	 */
	public static Properties getProperty(File file) {
		Properties p = new Properties();
		try {
			p.load(new FileReader(file));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return p;
	}

	/**
	 * 从指定类路径中获取Properties对象
	 * 
	 * @param classname
	 *            指定类路径，如：com/secondriver/util/config.properties
	 * @return Properties对象
	 */
	public static Properties getDefaultProperty(String classname) {
		ClassLoader loader = Thread.currentThread().getContextClassLoader();
		URL url = loader.getResource(classname);
		InputStream in;
		Properties p = new Properties();
		try {
			in = url.openStream();
			p.load(in);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return p;
	}
}
